package com.epam.kozich.model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "zakaz")
public class Order {
	private Long id;
	private Person person;
	private List<Book> books;
	private Calendar dateGetOrder;
	private Calendar dateReturnOrder;
	private Integer period;

	public Order() {
		new Order(null, 10);
	}

	public Order(Person person, Integer period) {
		this.person = person;
		this.period = period;
		//setDateGetOrder(GregorianCalendar.getInstance());
		//setDateReturnOrder(GregorianCalendar.getInstance());
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name="ORDER_ID")
	public Long getId() {
		return id;
	}

	@OneToOne(cascade = CascadeType.ALL)
	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@OneToMany(cascade = CascadeType.ALL, mappedBy = "order", fetch = FetchType.EAGER)
	public List<Book> getBooks() {
		return books;
	}

	public void setBooks(List<Book> books) {
		this.books = books;
	}

	public void addBookInOrder(Book book) {
		if (book.isEmpty() && !person.isBanned()) {
			if (books == null) {
				books = new ArrayList<Book>();
			}
			books.add(book);
			book.setEmpty(false);
		} else
			System.out.println("Error");
	}

	/*public void setDateGetOrder() {
		dateGetOrder.setTimeInMillis(System.currentTimeMillis());
		this.dateGetOrder = dateGetOrder;
	}

	@Column
	public Calendar getDateGetOrder() {
		return dateGetOrder;
	}

	public void setDateReturnOrder() {
		Calendar today = GregorianCalendar.getInstance();
		today.setTimeInMillis(System.currentTimeMillis());
		today.roll(Calendar.DAY_OF_MONTH, period);
		dateReturnOrder = today;
		this.dateReturnOrder = dateReturnOrder;
	}

	@Column
	public Calendar getDateReturnOrder() {
		Calendar today = GregorianCalendar.getInstance();
		today.setTimeInMillis(System.currentTimeMillis());
		today.roll(Calendar.DAY_OF_MONTH, period);
		dateReturnOrder = today;
		this.dateReturnOrder = dateReturnOrder;
		return dateReturnOrder;
	}*/
}
